<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增数据文件信息')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-datafile-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">文件状态：</label>
                <div class="col-sm-8">
                    <div class="radio-box" th:each="dict : ${@dict.getType('com_file_status')}">
                        <input type="radio" th:id="${'fileStatus_' + dict.dictCode}" name="fileStatus" th:value="${dict.dictValue}" th:checked="${dict.default}">
                        <label th:for="${'fileStatus_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">有效日期：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="invalidTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
			    <label class="col-sm-3 control-label">上传文件：</label>
			    <div class="col-sm-8">
			        <input id="fileinput" name="filePath"type="file" multiple>
			    </div>
			</div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        var prefix = ctx + "company/datafile"
        $("#form-datafile-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            /* if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-datafile-add').serialize());
            } */
        	uploadFile();
        }
        //上传方法
        function uploadFile() {
            var formData = new FormData();
            if ($('#fileinput')[0].files[0] == null) {
                $.modal.alertWarning("请先选择文件路径");
                return false;
            }
            for (var i=0;i<$('#fileinput')[0].files.length;i++) {
                /* formData.append('fileStatus', $("#fileStatus").val());
                formData.append('invalidTime', $("#invalidTime").val()); */
                formData.append('fileStatus', $("input[name='fileStatus']:checked").val());
                formData.append('invalidTime', $("input[name='invalidTime']").val());
                formData.append('file', $('#fileinput')[0].files[i]);
                $.ajax({
                    url: prefix + "/add",
                    type: 'post',
                    cache: false,
                    data: formData,
                    processData: false,
                    contentType: false,
                    dataType: "json",
                    success: function(result) {
                        $.operate.successCallback(result);
                    }
                });
                formData = new FormData();
            }

            $.modal.alertWarning("上传成功");
        }

        $("input[name='invalidTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
    </script>
    
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <script type="text/javascript">
    $(document).ready(function () {
        $("#fileinput").fileinput({
            'theme': 'explorer-fas',
            'uploadUrl': '#',
            overwriteInitial: false,
            initialPreviewAsData: true,
            showUpload: false
        });
        
        $("#fileinput").on("fileuploaded", function(event, data, proviewId, index) {
            console.info(event);
            console.info(data);
            console.info(proviewId);
            console.info(index);
        });
        
    });
    </script>
</body>
</html>